<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of admin
 *
 * @author QUYET
 */
class admin extends Admin_Controller {
    
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin_m');
        $this->load->library('pagination');
        $this->load->library('category');
        $this->load->library('form_validation');
        $this->load->helper('category_helper');
        $this->category->build_option_data(1, 'Categories', 0);
        
    }

    function index() {
        $this->listing();
    }

    function listing($id_category=1) {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['module_title'] = __('list_ticket');
        $this->_data['selected_cat'] = $id_category;
        
        $this->_data['category_options'] = $this->category->option_store;
        
        $cat_array = $this->category->children_array;
        $cat_array[] = $id_category;
        $off_set = $this->uri->segment(5) != '' ? $this->uri->segment(5) : 0;
        $per_page = 10;
        $config['full_tag_open'] = "<div class='page'>";
        $config['full_tag_close'] = '</div>';
        $config['cur_tag_open'] = '<span> ';
        $config['cur_tag_close'] = ' </span>';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $config['first_link'] = 'First';
        $config['last_link'] = 'Last';
        $config['base_url'] = base_url() . 'admin/tickets/listing/' . $id_category . '/';
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->admin_m->count_all_ticket($cat_array);
        
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['tickets'] = $this->admin_m->get_all_tickets($cat_array, $per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['id_cat'] = $id_category;
        $this->_data['off_set'] = $off_set;
        $this->display_admin('admin/listing');
    }

    function add() {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['module_title'] = __('add_ticket');
        //$this->category->build_option_data(1, 'Categories', 0);
        $this->_data['category_options'] = $this->category->option_store;
        if (isset($_POST['btnSubmit'])) {
            $this->form_validation->set_rules('ticket_name', __('ticket_name'), 'trim|required|max_length[300]|xss_clean');
            $this->form_validation->set_rules('price', __('ticket_price'), 'trim|required|greater_than[-1]|xss_clean');
            $this->form_validation->set_rules('quantity', __('ticket_quantity'), 'trim|required|is_natural|xss_clean');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run()) {
                $config['upload_path'] = './uploads/images/ticket/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = TRUE;
                $config['max_size'] = '524000';
                $config['max_width'] = '5000';
                $config['max_height'] = '3000';
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $this->_data['errors'] = $this->upload->display_errors();
                    $this->display_admin('admin/add');
                } else {
                    $data = array('upload_data' => $this->upload->data());
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = './uploads/images/ticket/' . $data['upload_data']['file_name'];
                    $config['new_image'] = './uploads/images/ticket/thumbnail/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 300;
                    $config['height'] = 200;
                    $this->load->library('image_lib', $config);
                    $this->image_lib->resize();
                    $this->db->insert('ots_image', array('img_name' => $data['upload_data']['file_name']));
                    $image_id = $this->db->insert_id();

                    $tiket_data = array('img_id' => $image_id);
                    $tiket_data['ticket_name'] = $_POST['ticket_name'];
                    $tiket_data['description'] = $_POST['description'];
                    $tiket_data['meta_title'] = $_POST['meta_title'];
                    $tiket_data['meta_description'] = $_POST['meta_description'];
                    $tiket_data['meta_keyword'] = $_POST['meta_keyword'];
                    $tiket_data['tag'] = $_POST['tag'];
                    $tiket_data['expiry_date'] = $_POST['expiry_date'];
                    $tiket_data['price'] = $_POST['price'];
                    $tiket_data['quantity'] = $_POST['quantity'];
                    $tiket_data['cat_id'] = $_POST['cat_id'];
                    if ($this->admin_m->add_ticket($tiket_data)) {
                        redirect('admin/tickets/listing/' . $tiket_data['cat_id']);
                    } else {
                        $this->_data['errors'] = 'An error has occured while adding the ticket!';
                        $this->display_admin('admin/add');
                    }
                }
            } else {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/add');
            }
        } else {
            $this->display_admin('admin/add');
        }
    }

    function edit($id_ticket) {
        $this->session->set_userdata('current_url', current_url());
        $this->_data['category_options'] = $this->category->option_store;
        $this->_data['module_title'] = __('admin_edit_for_ticket_id').' ' . $id_ticket;
        $ticket = $this->admin_m->get_details($id_ticket);
        if (!is_object($ticket)) {
            redirect('admin/tickets/listing');
        }
        $this->_data['ticket'] = $ticket;
        if (isset($_POST['btnEdit'])) {
            $this->form_validation->set_rules('ticket_name', __('ticket_name'), 'trim|required|max_length[300]|xss_clean');
            $this->form_validation->set_rules('price', __('ticket_price'), 'trim|required|greater_than[-1]|xss_clean');
            $this->form_validation->set_rules('quantity', __('ticket_quantity'), 'trim|required|is_natural|xss_clean');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
            if ($this->form_validation->run()) {
                $config['upload_path'] = './uploads/images/ticket/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = TRUE;
                $config['max_size'] = '5120';
                $config['max_width'] = '5000';
                $config['max_height'] = '3000';
                $this->load->library('upload', $config);
                if ($this->upload->do_upload()) {
                    //Delete old image
                    $old_img = './uploads/images/ticket/' . $ticket->img_name;
                    $old_thumb = './uploads/images/ticket/thumbnail/' . $ticket->img_name;
                    if ($ticket->img_name != '') {
                        if (file_exists($old_img)) {
                            unlink($old_img);
                        }
                        if (file_exists($old_thumb)) {
                            unlink($old_thumb);
                        }
                    }
                    //Change image
                    $data = array('upload_data' => $this->upload->data());
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = './uploads/images/ticket/' . $data['upload_data']['file_name'];
                    $config['new_image'] = './uploads/images/ticket/thumbnail/';
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 300;
                    $config['height'] = 200;
                    $this->load->library('image_lib', $config);
                    $this->image_lib->resize();
                    $this->db->update('ots_image', array('img_name' => $data['upload_data']['file_name']), array('id' => $ticket->img_id));
                } else{
                    $this->_data['errors'] = $this->upload->display_errors();
                    $this->display_admin('admin/edit');
                }
                $tiket_data = array();
                $tiket_data['ticket_name'] = $_POST['ticket_name'];
                $tiket_data['description'] = $_POST['description'];
                $tiket_data['meta_title'] = $_POST['meta_title'];
                    $tiket_data['meta_description'] = $_POST['meta_description'];
                    $tiket_data['meta_keyword'] = $_POST['meta_keyword'];
                    $tiket_data['tag'] = $_POST['tag'];
                $tiket_data['expiry_date'] = $_POST['expiry_date'];
                $tiket_data['price'] = $_POST['price'];
                $tiket_data['quantity'] = $_POST['quantity'];
                $tiket_data['cat_id'] = $_POST['cat_id'];
                $this->admin_m->update_ticket($tiket_data,$id_ticket);
                redirect('admin/tickets/listing/'.$ticket->cat_id);
            } else {
                $this->_data['errors'] = validation_errors();
                $this->display_admin('admin/add');
            }
        } else {

            $this->display_admin('admin/edit');
        }
    }
    
    function delete($id_cat=1,$id_ticket,$off_set=0){
        $tickets = $this->db->get_where('ots_ticket_serial',array('ticket_id'=>$id_ticket))->result_array();
        $new_expire_time  = date("Y-n-j H:i:s", time());
        if(count($tickets)>0){
            $this->db->update('ots_ticket',array('quantity'=>0,'expiry_date'=>$new_expire_time));
        } else{
            $this->admin_m->delete_ticket($id_ticket);
        }
        redirect(base_url().'admin/tickets/listing/'.$id_cat.'/'.$off_set);
    }
}

?>
